package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sys.model.po.SysMenu;
import ysq.xyg.web.dao.WebNavigationDao;
import ysq.xyg.web.model.po.WebNavigation;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebNavigationVo;
import ysq.xyg.web.service.WebNavigationService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 网站模块-网站导航service实现
 *
 * @author shengQiang yu
 * @date 2019-6-4 21:16:29
 */
@Service("webNavigationService")
public class WebNavigationServiceImpl extends BaseServiceImpl<WebNavigation> implements WebNavigationService {

    @Resource
    private WebNavigationDao webNavigationDao;

    private WebNavigation webNavigation;

    @Autowired
    public void setBaseDao(WebNavigationDao webNavigationDao) {
        this.baseDao = webNavigationDao;
    }


    /**
     * 分页查询
     *
     * @param webNavigationVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-4 21:16:29
     */
    @Override
    public ReturnEntity page(WebNavigationVo webNavigationVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,update_time desc");
        List<WebNavigation> list = acceptList(webNavigationVo);
        PageInfo<WebNavigation> page=new PageInfo<WebNavigation>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webNavigationVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-4 21:16:29
     */
    @Override
    public ReturnEntity addOrUpdate(WebNavigationVo webNavigationVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webNavigationVo.getId())){
            webNavigation=new WebNavigation(currentUser);
            /**
             * 获取导航的级别和code
             */
            if (StrUtil.isEmpty(webNavigationVo.getParentId())) {
                webNavigation.setGrade(BaseConstant.SYS_MENU_GRADE_ONE);
                //第一级
                WebNavigation bm = new WebNavigation();
                bm.setGrade(webNavigation.getGrade());
                bm.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
                String navCode = webNavigationDao.AcceptMaxNavCode(bm);
                if(StringUtil.isEmpty(navCode)){
                    webNavigation.setNavCode(BaseConstant.MENU_CODE_MIN);
                }else{
                    webNavigation.setNavCode(Long.parseLong(navCode)+1+"");
                }
            } else {
                WebNavigation parentNav = webNavigationDao.selectByPrimaryKey(webNavigationVo.getParentId());
                webNavigation.setGrade((Integer.parseInt(parentNav.getGrade())+1)+"");
                WebNavigation bm = new WebNavigation();
                bm.setParentId(parentNav.getId());
                bm.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
                String navCode = webNavigationDao.AcceptMaxNavCode(bm);
                if(StringUtil.isEmpty(navCode)){
                    webNavigation.setNavCode(parentNav.getNavCode()+BaseConstant.MENU_CODE_MIN);
                }else{
                    webNavigation.setNavCode(Long.parseLong(navCode)+1+"");
                }
            }
            isUpate=false;
        }else{
            webNavigation=selectByPrimaryKey(webNavigationVo.getId());
            if(ObjectUtil.isNull(webNavigation)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webNavigation.setUpdateTime(new Date());
            webNavigation.setUpdateUser(currentUser.getId());
        }
        webNavigation.setNavIconType(webNavigationVo.getNavIconType());
        webNavigation.setUseType(webNavigationVo.getUseType());
        webNavigation.setName(webNavigationVo.getName());
        webNavigation.setNavType(webNavigationVo.getNavType());
        webNavigation.setUrl(webNavigationVo.getUrl());
        webNavigation.setParentId(webNavigationVo.getParentId());
        webNavigation.setSort(webNavigationVo.getSort());
        webNavigation.setShowType(webNavigationVo.getShowType());
        webNavigation.setUserId(currentUser.getId());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webNavigation);
        }else{
            num=insert(webNavigation);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-4 21:16:29
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebNavigation webNavigation=selectByPrimaryKey(id);
        if(webNavigation!=null){
            webNavigation.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webNavigation.setUpdateTime(new Date());
            webNavigation.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webNavigation);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }
    /**
     * 获取菜单及子菜单
     *
     * @param webNavigationVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-6-4 21:16:29
     */
    @Override
    public List<WebNavigation> acceptNavMenuList(WebNavigationVo webNavigationVo){
        List<WebNavigation> webNavigationList=acceptList(webNavigationVo);
        //获取一级菜单
        List<WebNavigation> oneMenu=new ArrayList<WebNavigation>();
        if(webNavigationList!=null&&webNavigationList.size()>0){
            for(WebNavigation webNavigation:webNavigationList){
                if(BaseConstant.SYS_MENU_GRADE_ONE.equals(webNavigation.getGrade())){
                    //如果是一级菜单
                    oneMenu.add(getChildMenu(webNavigationList,webNavigation));
                }
            }
        }
        return oneMenu;
    }

    private WebNavigation getChildMenu(List<WebNavigation> list,WebNavigation currentMenu) {
        //如果当前菜单类型不为目录
        if(!(BaseConstant.SYS_MENU_TYPE_DIC.equals(currentMenu.getNavType()))){
            return currentMenu;
        }
        //子类菜单
        List<WebNavigation> childMenu=new ArrayList<WebNavigation>();
        if(list!=null&&list.size()>0){
            for(WebNavigation menu:list){
                if(currentMenu.getId().equals(menu.getParentId())){
                    menu=getChildMenu(list,menu);
                    childMenu.add(menu);
                }
            }
        }
        currentMenu.setChildNavMenuList(childMenu);
        return currentMenu;
    }

    /**
     * 获取集合
     *
     * @param webNavigationVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-6-4 21:16:29
     */
    @Override
    public List<WebNavigation> acceptList(WebNavigationVo webNavigationVo){
        Example example = new Example(WebNavigation.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webNavigationVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webNavigationVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(webNavigationVo.getNavType())){
            criteria.andEqualTo("navType",webNavigationVo.getNavType());
        }
        if(StringUtil.isNotEmpty(webNavigationVo.getUseType())){
            criteria.andEqualTo("useType",webNavigationVo.getUseType());
        }
        if(StringUtil.isNotEmpty(webNavigationVo.getShowType())){
            criteria.andEqualTo("showType",webNavigationVo.getShowType());
        }
        if(StringUtil.isNotEmpty(webNavigationVo.getName())){
            criteria.andLike("name","%"+webNavigationVo.getName()+"%");
        }
        if(StringUtil.isNotEmpty(webNavigationVo.getUserId())){
            criteria.andEqualTo("userId",webNavigationVo.getUserId());
        }
        example.setOrderByClause(" sort desc, update_time desc");
        return webNavigationDao.selectByExample(example);
    }
}
